﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;

using System.Text;
using System.Windows.Forms;

namespace Controls
{
    [DefaultEvent("PageIndexChanged"), DefaultProperty("RecordCount"), Description("WinForm下的分页控件")]
    public partial class PsPager : UserControl
    {
        private string PagerText = "总共{0}条记录,当前第{1}页,共{2}页,每页{3}条记录";
        private int _sumPage = 0;

        [Description("更改页面索引事件"), Category("自定义事件")]
        public event EventHandler PageIndexChanged;

        private int _pageSize = 10;

        [DefaultValue(10)]
        public int PageSize
        {
            get { return _pageSize; }
            set
            {
                _pageSize = value;
                if (_pageSize == 0)
                    throw new Exception("每页记录数不能为0！");
            }
        }

        private int _rowCount = 0;

        [DefaultValue(0)]
        public int RowCount
        {
            get { return _rowCount; }
            set
            {
                _rowCount = value;
                GetSumPage();
                SetPagerText(); //刷新页面显示
            }
        }

        private int _pageIndex = 1;

        [DefaultValue(1)]
        public int PageIndex
        {
            get { return _pageIndex; }
            set { _pageIndex = value; }
        }

        public PsPager()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            GetSumPage();
            SetPagerText();
            SetBtnEnabled();//设置按钮是否能够启用
        }

        private void CustomEvent(object sender, EventArgs e)
        {
            try
            {
                this.PageIndexChanged(sender, e);
            }
            catch (Exception)
            {
                //MessageBox.Show("未找到PageIndexChanged事件！");
            }
        }


        /// <summary>
        /// 设置文字
        /// </summary>
        private void SetPagerText()
        {
            this.lbPageInfo.Text = string.Format(PagerText,
                this.RowCount, this.PageIndex, _sumPage, this.PageSize);

            SetBtnEnabled();
        }
        //得到总页数
        private void GetSumPage()
        {
            if (this.RowCount <= 0) _sumPage = 0;

            _sumPage = this.RowCount / this.PageSize;
            if (this.RowCount % this.PageSize != 0) _sumPage++;
        }

        public void SetBtnEnabled()
        {
            if (_sumPage < 2) //没有页
            {
                this.btnEnd.Enabled = false;
                this.btnFirst.Enabled = false;
                this.btnNext.Enabled = false;
                this.btnPre.Enabled = false;
            }
            else if (this.PageIndex == 1)
            {
                this.btnFirst.Enabled = false;
                this.btnPre.Enabled = false;
                this.btnNext.Enabled = true;
                this.btnEnd.Enabled = true;
            }
            else if (this.PageIndex == _sumPage) //当前页是最后一页
            {
                this.btnFirst.Enabled = true;
                this.btnPre.Enabled = true;
                this.btnNext.Enabled = false;
                this.btnEnd.Enabled = false;
            }
            else
            {
                this.btnEnd.Enabled = true;
                this.btnPre.Enabled = true;
                this.btnFirst.Enabled = true;
                this.btnNext.Enabled = true;
            }
        }

        public void BtnFirstClick()
        {
            if (!isCanEdit(this.btnFirst)) return;

            this.btnFirst.PerformClick();
        }

        public void BtnPreClick()
        {
            if (!isCanEdit(this.btnPre)) return;

            this.btnPre.PerformClick();
        }

        public void BtnNextClick()
        {
            if (!isCanEdit(this.btnNext)) return;

            this.btnNext.PerformClick();
        }

        public void BtnEndClick()
        {
            if (!isCanEdit(this.btnEnd)) return;

            this.btnEnd.PerformClick();
        }

        private void btnFirst_Click(object sender, EventArgs e)
        {
            GetSumPage();
            this._pageIndex = 1;
            this.SetPagerText();
            this.SetBtnEnabled();
            this.CustomEvent(sender, e);
        }

        private void btnPre_Click(object sender, EventArgs e)
        {
            GetSumPage();
            this._pageIndex -= 1;
            this.SetPagerText();
            this.SetBtnEnabled();
            this.CustomEvent(sender, e);
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            GetSumPage();
            this._pageIndex += 1;
            this.SetPagerText();
            this.SetBtnEnabled();
            this.CustomEvent(sender, e);
        }

        private void btnEnd_Click(object sender, EventArgs e)
        {
            GetSumPage();
            this._pageIndex = _sumPage;
            this.SetPagerText();
            this.SetBtnEnabled();
            this.CustomEvent(sender, e);
        }

        private bool isCanEdit(Button btn)
        {
            if (null == btn || btn.Visible == false
                || btn.Enabled == false)
            {
                return false;
            }

            return true;
        }
    }
}
